package com.lanju.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanju.model.ResultModel;
import com.lanju.model.ResultStatus;
import com.lanju.model.SAModel;
import com.lanju.service.SystemAdminService;

@Controller
@RequestMapping("/sysAdimn")
public class SysAdminController {
    
    @Autowired
    SystemAdminService systemAdminService;
    
    /**
     * 生成
     * */
    @RequestMapping("/createAdmin")
    @ResponseBody
    public void createAdmin() {
        systemAdminService.createAdmin();
    }
    
    /**
     * 注册
     * */
    @RequestMapping("/adminRegister")
    @ResponseBody
    public ResultModel adminRegister(
            @RequestParam("adminName") String adminName,
            @RequestParam("sex") Short sex,
            @RequestParam("occupation") String occupation,
            @RequestParam("organizeCode") String organizeCode,
            @RequestParam("flag") Integer flag,
            HttpServletRequest request,
            HttpSession httpSession) {
        SAModel systemAdminModel = (SAModel) httpSession.getAttribute("saModel");
        if(systemAdminModel==null){
            return ResultModel.fail("会话过期，请重新登录", null);
        }
        return systemAdminService.adminRegister(adminName,sex,occupation,systemAdminModel,organizeCode,flag);
        
    }
    
    
    /**
     * 登录
     * */
    @RequestMapping("/login")
    @ResponseBody
    public ResultModel login(
            @RequestParam("adminName") String adminName,
            @RequestParam("password") String password,
            HttpSession httpSession){
        ResultModel model = systemAdminService.login(adminName,password);
        if(model.getResultCode()==ResultStatus.RESULT_STATUS_SUCCESS.getValue()){
            SAModel sysModel = (SAModel) model.getContent();
            httpSession.setAttribute("saModel", sysModel);
        }
        return model;
        
    }
    
    /**
     * 退出
     * */
    @RequestMapping("/loginout")
    @ResponseBody
    public ResultModel loginOut(HttpSession httpSession) {
        if (httpSession.getAttribute("saModel") != null) {
            httpSession.removeAttribute("saModel");
        }
        return ResultModel.success();
    }
    
    /**
     * 管理员列表
     * */
    @RequestMapping("/getAdminList")
    @ResponseBody
    public Map<String, Object> getAdminList(
            @RequestParam(value="adminName",required=false) String adminName,
            @RequestParam("limit") int pageSize,
            @RequestParam("offset") int offset){
                return systemAdminService.getAdminList(adminName,pageSize,offset);
        
    }
    
    /**
     * 编辑管理员
     * */
    @RequestMapping("/editAdmin")
    @ResponseBody
    public ResultModel editAdmin(
           @RequestParam("adminCode") String adminCode,
           @RequestParam(value="adminName",required=false) String adminName,
           @RequestParam(value="sex",required=false) Short sex,
           @RequestParam(value="occupation",required=false) String occupation,
           @RequestParam("organizeCode") String organizeCode,
           @RequestParam(value="flag",defaultValue="0") Integer flag,
           HttpServletRequest request,
           HttpSession httpSession){
        SAModel systemAdminModel = (SAModel) httpSession.getAttribute("saModel");
        if(systemAdminModel!=null){
            if(systemAdminModel.getFlag()!=1){
                return ResultModel.fail("您非超级管理员！", null);
            }
        }else{
            return ResultModel.fail("会话过期，请重新登录", null);
        }
        return systemAdminService.eidtAdmin(adminCode,adminName,sex,occupation,organizeCode,flag);
        
    }
    
    /**
     * 删除管理员
     * */
    @RequestMapping("/deleteAdmin")
    @ResponseBody
    public ResultModel deleteAdmin(
           @RequestParam("adminCode") String adminCode,
           HttpServletRequest request,
           HttpSession httpSession){
        SAModel systemAdminModel = (SAModel) httpSession.getAttribute("saModel");
        if(systemAdminModel!=null){
            if(systemAdminModel.getFlag()!=1){
                return ResultModel.fail("您非超级管理员！", null);
            }
        }else{
            return ResultModel.fail("会话过期，请重新登录", null);
        }
        return systemAdminService.deleteAdmin(adminCode);
        
    }
    
    /**
     * 修改密码
     * */
    @RequestMapping("/editPwd")
    @ResponseBody
    public ResultModel editPwd(
           @RequestParam("pwd") String pwd,
           @RequestParam("oldPwd") String oldPwd,
           HttpServletRequest request,
           HttpSession httpSession){
        SAModel systemAdminModel = (SAModel) httpSession.getAttribute("saModel");
        if(systemAdminModel==null){
            return ResultModel.fail("会话过期，请重新登录", null);
        }
        return systemAdminService.editPwd(pwd,oldPwd,systemAdminModel.getAdminCode());
        
    }
    
    /**
     * 获取flag为2的团队管理员
     * */
    @RequestMapping("/getOrganizeInAdmin")
    @ResponseBody
    public ResultModel getOrganizeInAdmin(){
        return systemAdminService.getOrganizeInAdmin();
        
    }
    
}
